﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace cs2335lab3grp1
{
    /// <summary>
    /// This class provides methods for reading in XML files
    /// and running Gradebook reports on them.
    /// </summary>
    public class Gradebook
    {
        #region variables and properties
        private StudentList studentlist;
        public StudentList StudentList
        {
            get { return studentlist; }
            set { studentlist = value; }
        }

        private string classname;
        public string Classname
        {
            get { return classname; }
            set { classname = value; }
        }
        #endregion
        #region methods
        /// <summary>
        /// This method reads in an XML file from a TextReader parameter.
        /// </summary>
        /// <param name="tr">A TextReader object containing the XML file.</param>
        public void ReadXML(TextReader tr)
        {
            //now read in an entire xml document at once.
            XmlSerializer deserial = new XmlSerializer(typeof(StudentList));
            studentlist = (StudentList)deserial.Deserialize(tr);
        }

        /// <summary>
        /// This method reads in an XML file from a filename in a string parameter.
        /// </summary>
        /// <param name="filename">A string containing the filename to be read.</param>
        public void ReadXML(string filename)
        {
            //now read in an entire xml document at once.
            TextReader read = new StreamReader(filename);
            ReadXML(read);
        }

        /// <summary>
        /// Returns the gradebook report from the student list.
        /// </summary>
        /// <returns>A string with the grade report.</returns>
        public string ShowReport()
        {
            string output = "";

            // if it exists, return the student list
            if (studentlist != null)
            {
                output = studentlist.ToString();
            }

            return output;
        }
        #endregion methods
        #region main

        /// <summary>
        /// Reads an XML file called "students.xml" and runs a 
        /// Gradebook report on the students contained in the file.
        /// </summary>
        /// <param name="args">Arguments to the program</param>
        static void Main(string[] args)
        {
            // our xml file
            string fn = "students.xml";

            // read in the xml file
            Gradebook g = new Gradebook();
            g.ReadXML(fn);

            // write out the report
            Console.WriteLine(g.ShowReport());

            // wait for user interaction
            Console.Read();
        }
        #endregion main
    }
}
